package com.one.controller;

import com.one.dto.PageResponseDto;
import com.one.dto.SupervisorLoginDTO;
import com.one.dto.SupervisorPageRequestDTO;
import com.one.dto.SupervisorResetDTO;
import com.one.entity.Statistics;
import com.one.entity.Supervisor;
import com.one.properties.JwtProperties;
import com.one.result.Result;
import com.one.service.SupervisorService;
import com.one.utils.JwtUtil;
import com.one.vo.SupervisorLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Api(tags = "监督员相关接口")
@Slf4j
@RestController
@RequestMapping("/supervisor")
public class SupervisorController {
    @Autowired
    private SupervisorService supervisorService;

    @Autowired
    private JwtProperties jwtProperties;

    @ApiOperation(value = "监督员登录接口")
    @PostMapping("/getSupervisorByIdByPass")
    public SupervisorLoginVO login(@RequestBody SupervisorLoginDTO supervisorLoginDTO) {
        log.info("监督员登录请求{}",supervisorLoginDTO);
        Supervisor supervisor=supervisorService.login(supervisorLoginDTO);
        if (supervisor == null) {
            return null; // 登录失败，返回null
        }
        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put("telId", supervisor.getTelId());
        claims.put("realName", supervisor.getRealName());
        String token = JwtUtil.createJWT(
                jwtProperties.getUserSecretKey(),
                jwtProperties.getUserTtl(),
                claims);
        // 封装返回结果(监督员信息和token令牌)
        SupervisorLoginVO supervisorLoginVO = SupervisorLoginVO.builder()
                .telId(supervisor.getTelId())
                .realName(supervisor.getRealName())
                .birthday(supervisor.getBirthday())
                .sex(supervisor.getSex())
                .token(token)
                .build();
        return supervisorLoginVO;
    }

    @ApiOperation(value = "监督员注册")
    @PostMapping("/saveSupervisor")
    public int register(@RequestBody Supervisor supervisor) {
        log.info("监督员注册请求: {}", supervisor);
        return supervisorService.saveSupervisor(supervisor);
    }

    @ApiOperation(value="公众监督员重置密码")
    @PostMapping("/resetPassword")
    public int resetPassword(@RequestBody SupervisorResetDTO supervisorResetDTO){
        log.info("公众监督员重置密码：{}",supervisorResetDTO);
        return supervisorService.resetPassword(supervisorResetDTO);
    }

    @ApiOperation(value = "根据手机号获取监督员信息")
    @GetMapping("/getSupervisorById")
    public Supervisor getSupervisorById(@RequestParam String telId) {
        log.info("获取监督员信息请求，手机号: {}", telId);
        //无需检查是否为null，前端已做处理
        return supervisorService.getSupervisorById(telId);
    }

    @PostMapping("/getSupervisorList")
    @ApiOperation(value = "监督员信息分页查询")
    public PageResponseDto<Supervisor> getSupervisorList(@RequestBody SupervisorPageRequestDTO supervisorPageRequestDTO) {
        log.info("监督员信息分页查询请求: {}", supervisorPageRequestDTO);
        return supervisorService.getSupervisorList(supervisorPageRequestDTO);
    }

    @PostMapping("/updateSupervisor")
    @ApiOperation(value = "更新监督员信息")
    public int updateSupervisor(@RequestBody Supervisor supervisor) {
        log.info("更新监督员信息请求: {}", supervisor);
        return supervisorService.updateSupervisor(supervisor);
    }

    @PostMapping("/deleteSupervisor")
    @ApiOperation(value = "删除监督员信息")
    public int deleteSupervisor(@RequestParam String telId) {
        log.info("删除监督员信息请求，手机号: {}", telId);
        return supervisorService.deleteBytelId(telId);
    }



}
